from flask import current_app
import redis

class Redis(object):
    """
    redis数据库操作
    """
    @staticmethod
    def _get_r():
        host = current_app.config['REDIS_HOST']
        port = current_app.config['REDIS_PORT']
        db = current_app.config['REDIS_DB']
        r = redis.StrictRedis(host, port,db)
        return r



    @classmethod
    def read(cls,key):
        """
    	读取键值对内容
    	"""
        r = cls._get_r()
        value = r.get(key)
        return value.decode('utf-8') if value else value


    @classmethod
    def rank(cls,key):
        r = cls._get_r()
        o = r.zrange(key, 0, 9, withscores=True, desc=True)
        return o

    @classmethod
    def all(cls,key):
        r = cls._get_r()
        e = r.zrange(key, 0, -1, withscores=True)
        return e

    @classmethod
    def count(cls,key):
        r = cls._get_r()
        a = r.scard(key)
        return str(a)

    @classmethod
    def city_rank(cls, key):
        r = cls._get_r()
        a = r.zrange(key,-10,-1,withscores=True,byscore=False)
        return a